﻿using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Hubs;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace WinClient
{
    public partial class FrmClient : Form
    {
        public FrmClient()
        {
            InitializeComponent();
        }
        public void WriteLog(object obj)
        {
            this.Invoke(new Action(() =>
            {
                txtMessage.AppendText($"{DateTime.Now:HH:mm:ss}：{obj}\r\n\r\n");
            }));
        }

        /// <summary>
        /// 连接代理对象
        /// </summary>
        private IHubProxy hubProxy { get; set; }

        /// <summary>
        /// 连接对象
        /// </summary>
        private HubConnection hubConnection { get; set; }
        /// <summary>
        /// 连接Hub
        /// </summary>
        private async void ConnHub()
        {
            var param = new Dictionary<string, string> {
                { "Param", "str" }
            };
            //signalR的服务器地址
            var signalUrl = "http://localhost:1245";
            //创建连接对象
            hubConnection = new HubConnection(signalUrl, param);
            //hubConnection.Received += HubConnection_Received;
            //绑定一个集线器
            hubProxy = hubConnection.CreateHubProxy("MyHub");
            
            
            //注册收到服务端数据的方法,此方法请转至服务端MyHub.cs中查看
            hubProxy.On("Update", (a) =>
            {
                WriteLog($"Update-来自服务器的消息：{a}");
            });
            hubProxy.On("SayHello", (a) =>
            {
                WriteLog($"SayHello-来自服务器的消息：{a}");
            });
            hubProxy.On("SendClose", (a) =>
            {
                WriteLog($"SendClose-来自服务器的消息：{a}");
            });
            //开始连接（必须放On后面，否则一连上，接收不到服务端发来的消息）
            await hubConnection.Start();
        }

        private void HubConnection_Received(string obj)
        {
            WriteLog($"Received服务器的信息：{obj}");
        }

        private void FrmClient_Load(object sender, EventArgs e)
        {

        }

        private void btnConnection_Click(object sender, EventArgs e)
        {
            ConnHub();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string msg = txtSend.Text;
            string id = txtID.Text;
            string strMsg = $"{id}-{msg}";
            hubProxy.Invoke("Send", strMsg);
            WriteLog($"发送消息成功：{strMsg}");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string msg = txtSend.Text;
            string id = txtID.Text;
            string strMsg = $"{id}-{msg}";
            WriteLog($"发送消息成功：{strMsg}");
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (hubConnection != null)
            {
                hubConnection.Stop();
                hubConnection.Dispose();

            }
        }
    }
}
